//
//  OperationBillDiaoDuVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/11/9.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "OperationBillDiaoDuVC.h"
#import "DriverAddCell.h"
#import "OperationBillDiaoDuModel.h"
#import "OperationBillChooseDriverVC.h"
#import "OperationBillChooseCarVC.h"
#import "TaskDiaoDuOrderVC.h"
#import "TaskDiaoDuOrderModel.h"
#import "OperationBillDiaoDuGoodInfoCell.h"

@interface OperationBillDiaoDuVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) OperationBillDiaoDuModel *model;
@property (nonatomic, strong) NSMutableArray <TaskDiaoDuOrderModel *>*dataArray;

@end

@implementation OperationBillDiaoDuVC

-(NSMutableArray<TaskDiaoDuOrderModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(OperationBillDiaoDuModel *)model {
    if (!_model) {
        _model = [OperationBillDiaoDuModel new];
        _model.goods_num = self.goods_num;
        _model.goods_jnum = self.goods_jnum;
        _model.weight = self.goods_weight;
        _model.volume = self.goods_volume;
        _model.delivery_address = self.delivery_address;
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"调度";
    [self.view addSubview:self.tableView];
    UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 88, 30)];
    [editBtn setTitle:@"确认调度" forState:0];
    [editBtn setTitleColor:HexColor(0xffffff) forState:0];
    editBtn.titleLabel.font = BoldFontOfSize(14);
    editBtn.backgroundColor = Color_ThemeColor;
    editBtn.layer.masksToBounds = YES;
    editBtn.layer.cornerRadius = 15;
    [editBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierTaskDiaoDuOrders params:@{@"sta_id":@(self.sta_id)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.dataArray = [TaskDiaoDuOrderModel mj_objectArrayWithKeyValuesArray:responseObject];
            [self.dataArray enumerateObjectsUsingBlock:^(TaskDiaoDuOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [obj.goods_list enumerateObjectsUsingBlock:^(TaskDiaoDuOrderGoodModel * _Nonnull g_obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    g_obj.select_pnum = g_obj.residue_pnum;
                    g_obj.select_num = g_obj.residue_num;
                    g_obj.select_weight = g_obj.residue_weight;
                    g_obj.select_volume = g_obj.residue_volume;
                }];
            }];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

//确认调度
- (void)sureClick {
    if (!self.model.d_id) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请选择司机"];
        return;
    }
    if ([FactoryMethod isNull:self.model.d_mobile]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请填写司机手机"];
        return;
    }
    if ([FactoryMethod isNull:self.model.v_num]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请选择车辆"];
        return;
    }
    if (!self.model.goods_jnum) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入装载件数"];
        return;
    }
    if ([FactoryMethod isNull:self.model.delivery_address]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请填写送货地址"];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(self.sta_id) forKey:@"sta_id"];
    [dic setObject:@(self.model.d_id) forKey:@"s_id"];
    [dic setObject:self.model.v_num forKey:@"car_num"];
    [dic setObject:@(self.model.goods_jnum) forKey:@"pnum"];
    [dic setObject:@(self.model.goods_num) forKey:@"num"];
    [dic setObject:@(self.model.weight) forKey:@"weight"];
    [dic setObject:@(self.model.volume) forKey:@"volume"];
    NSMutableArray *goods = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(TaskDiaoDuOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dd1 = [NSMutableDictionary dictionary];
        [dd1 setObject:@(obj.dp_id) forKey:@"dp_id"];
        NSMutableArray *goodArr = [NSMutableArray array];
        [obj.goods_list enumerateObjectsUsingBlock:^(TaskDiaoDuOrderGoodModel * _Nonnull g_obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMutableDictionary *dd2 = [NSMutableDictionary dictionary];
            [dd2 setObject:@(g_obj.g_id) forKey:@"g_id"];
            [dd2 setObject:@(g_obj.select_pnum) forKey:@"pnum"];
            [dd2 setObject:@(g_obj.select_num) forKey:@"num"];
            [dd2 setObject:@(g_obj.select_weight) forKey:@"weight"];
            [dd2 setObject:@(g_obj.select_volume) forKey:@"volume"];
            [goodArr addObject:dd2];
        }];
        [dd1 setObject:goodArr forKey:@"goods_list"];
        [goods addObject:dd1];
    }];
    if (goods.count) {
        [dic setObject:[FactoryMethod stringWithJSONData:goods] forKey:@"goods"];
    }
    
    [HttpRequest PostWithApi:LogisticsSupplierTaskDiaoDu params:dic progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"调度成功" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshTaskManagerList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
    __weak_self;
    if (indexPath.row == 0) {
        cell.titleText = @"司机姓名";
        cell.showLine = YES;
        cell.canClick = YES;
        cell.isMust = YES;
        cell.contentText = weakself.model.d_name;
    }else if (indexPath.row == 1) {
        cell.titleText = @"司机手机";
        cell.showLine = YES;
        cell.canClick = NO;
        cell.isMust = YES;
        cell.contentText = weakself.model.d_mobile;
        cell.cellBlock = ^(NSString * _Nonnull str) {
            weakself.model.d_mobile = str;
        };
    }else if (indexPath.row == 2) {
        cell.titleText = @"车牌号";
        cell.showLine = YES;
        cell.canClick = YES;
        cell.isMust = YES;
        cell.contentText = weakself.model.v_num;
    }else if (indexPath.row == 3) {
        OperationBillDiaoDuGoodInfoCell *cell1 = [OperationBillDiaoDuGoodInfoCell OperationBillDiaoDuGoodInfoCellWithTableView:tableView];
        cell1.model = self.model;
        return cell1;
    }else {
        cell.titleText = @"送货地址";
        cell.showLine = YES;
        cell.canClick = NO;
        cell.isMust = YES;
        cell.contentText = weakself.model.delivery_address;
        cell.canNOUserOperation = YES;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3) {
        OperationBillDiaoDuGoodInfoCell *cell1 = [OperationBillDiaoDuGoodInfoCell OperationBillDiaoDuGoodInfoCellWithTableView:tableView];
        return cell1.height;
    }else {
        DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
        return cell.height;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        OperationBillChooseDriverVC *vc = [OperationBillChooseDriverVC new];
        __weak_self;
        vc.finishBlock = ^(OperationBillChooseDriverModel * _Nonnull driverModel) {
            weakself.model.d_id = driverModel.driver_sid;
            weakself.model.d_name = driverModel.name;
            weakself.model.d_mobile = driverModel.phone;
            weakself.model.v_num = driverModel.car_num;
            [weakself.tableView reloadData];
        };
        [self.rt_navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 2) {
        OperationBillChooseCarVC *vc = [OperationBillChooseCarVC new];
        __weak_self;
        vc.finishBlock = ^(int v_id, NSString * _Nonnull num) {
            weakself.model.v_id = v_id;
            weakself.model.v_num = num;
            [weakself.tableView reloadData];
        };
        [self.rt_navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 3) {
        TaskDiaoDuOrderVC *vc = [TaskDiaoDuOrderVC new];
        vc.sta_id = self.sta_id;
        vc.dataArray = self.dataArray;
        __weak_self;
        vc.finishBlock = ^{
            __block int pnum = 0;
            __block int num = 0;
            __block CGFloat weight = 0.0;
            __block CGFloat volume = 0.0;
            [weakself.dataArray enumerateObjectsUsingBlock:^(TaskDiaoDuOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [obj.goods_list enumerateObjectsUsingBlock:^(TaskDiaoDuOrderGoodModel * _Nonnull g_obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    pnum += g_obj.select_pnum;
                    num += g_obj.select_num;
                    weight += g_obj.select_weight;
                    volume += g_obj.select_volume;
                }];
            }];
            weakself.model.goods_jnum = pnum;
            weakself.model.goods_num = num;
            weakself.model.weight = weight;
            weakself.model.volume = volume;
            [tableView reloadData];
        };
        [self.rt_navigationController pushViewController:vc animated:YES];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

@end
